<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>请假信息管理系统 -- 填写请假申请</title>
    <script src="../../../js/jquery.min.js"></script>
    <link href="../../../css/leaveApplyManage/addLeaveApply.css" rel="stylesheet">
    <link href="../../../css/common/common.css" rel="stylesheet">
</head>
<body>
<!-- 顶部弹出提示信息容器 -->
<div id="notification" class="notification">
    <span id="message"></span>
</div>
<!-- 头部 -->
<header th:insert="~{common/common::header}">
</header>
<!-- 左边 -->
<aside th:insert="~{common/common::aside}">
</aside>
<!-- 中间 -->
<main class="main">
    <form id="leaveApplyFrom" action="/leaveApply/addLeaveApply" method="post">
        <h2 style="margin: 20px 30%;">填写请假申请</h2>
        <input name="stuNo" th:value="${session.username}" style="display: none">
        <div class="form-group">
            <label for="leaveID">申请编号</label>
            <input type="text" id="leaveID" name="leaveID" required readonly="readonly" th:value="${leaveID}">
        </div>
        <div class="form-group">
            <label for="courseId">课程名称
                <select id="courseId" name="courseID">
                    <option>请选择课程</option>
                </select>
            </label>
        </div>

        <div class="form-group">
            <label for="dayNum">请假天数</label>
            <input type="number" id="dayNum" name="dayNum" style="width: 500px;height: 40px;">
        </div>
        <div class="form-group">
            <label for="courseReason">请假事由</label>
            <textarea id="courseReason" name="reason"></textarea>
        </div>
        <input type="button" value="返回" onclick="goBack()">
        <input type="button" value="保存提交" onclick="addLeaveApply()">
    </form>

</main>
</body>

<script th:inline="javascript" type="text/javascript">
    function goBack(){
        window.location.href = '/leaveApply/list?pageSize=7&pageNum=1'
    }
    var classList = [[${courses}]];
    var className = []
    classList.forEach(item => {
        className.push({
            "label" : item.courseName,
            "value" : item.courseID
        })
    })
    var selectElement = document.getElementById("courseId");
    className.forEach(option => {
        var optionElement = document.createElement("option");
        optionElement.value = option.value;
        optionElement.textContent = option.label;
        selectElement.appendChild(optionElement);
    })

    function addLeaveApply(){
        var dayNum = parseInt(document.getElementById("dayNum").value);
        document.getElementById("dayNum").value = dayNum;
        if(dayNum.length == 0){
            document.getElementById("message").innerHTML = "请假天数不能为空，请重新输入";
            this.showNotification();
            return false;
        }
        if(dayNum <= 0){
            document.getElementById("message").innerHTML = "请假天数不正确，请重新输入";
            this.showNotification();
            return false;
        }
        var courseId = document.getElementById("courseId").value;
        if (courseId == "请选择课程"){
            document.getElementById("message").innerHTML = "请选择课程";
            this.showNotification();
            return false;
        }
        document.getElementById("leaveApplyFrom").submit();
    }

    function showNotification() {
        const notification = document.getElementById("notification");
        notification.classList.add("show");

        setTimeout(function() {
            notification.classList.remove("show");
        }, 1000); // 3秒后自动消失
    }

    function outLogin(){
        location.href = '/outLogin'
    }
</script>

</html>